import 'package:flutter/material.dart';
import 'package:trip_flutter/dao/loginDao.dart';
import 'package:trip_flutter/util/emptyUtil.dart';
import 'package:trip_flutter/util/spaceBox.dart';
import 'package:trip_flutter/widget/loginButton.dart';
import 'package:url_launcher/url_launcher.dart';

import '../util/navigate_util.dart';
import '../widget/inputWidget.dart';

class Loginpage extends StatefulWidget {
  const Loginpage({super.key});

  @override
  State<Loginpage> createState() => _LoginpageState();
}

class _LoginpageState extends State<Loginpage> {
  //设置登录时的变量
  bool enableLogin = false;
  String? loginName;
  String? loginPassword;

  @override
  Widget build(BuildContext context) {
    NavigatorUtil.upgradeContext(context);
    return Scaffold(
      resizeToAvoidBottomInset: false,
      body: Stack(children: [..._background(), _content()]),
    );
  }

  _background() {
    return [
      Positioned.fill(
        child: Image.asset('images/login-bg1.jpg', fit: BoxFit.cover),
      ),
      Positioned.fill(
        child: Container(decoration: BoxDecoration(color: Colors.black54)),
      ),
    ];
  }

  _content() {
    return Positioned.fill(
      left: 25,
      right: 25,
      child: ListView(
        children: [
          spaceBox(h: 50),
          Text("账号密码登录", style: TextStyle(fontSize: 32, color: Colors.white)),
          spaceBox(h: 100),
          Inputwidget(
            "请输入账号",
            hidePassword: false,
            onChange: (text) {
              loginName = text;
              _checkInput();
              print(enableLogin);
            },
          ),
          spaceBox(h: 10),
          Inputwidget(
            "请输入密码",
            onChange: (text) {
              loginPassword = text;
              _checkInput();
              print(enableLogin);
            },
          ),
          spaceBox(h: 50),
          loginButton(
            "登录",
            enable: enableLogin,
            onPressed: () => _login(context),
          ),
          spaceBox(h: 10),
          Align(
            alignment: Alignment.centerRight,
            child: InkWell(
              onTap: _registeredtUser,
              child: Text(
                "注册用户",
                style: TextStyle(color: Colors.white, fontSize: 16),
              ),
            ),
          ),
        ],
      ),
    );
  }

  void _checkInput() {
    setState(() {
      if (isEmpty(loginName) || isEmpty(loginPassword)) {
        enableLogin = false;
      } else {
        enableLogin = true;
      }
    });
  }

  void _login(BuildContext context) async {
    //实现登录逻辑
    await LoginDao.login(loginName!, loginPassword!);
    // print(pre.getString('user'));
    var history = LoginDao.getPermit();
    if (history != null) {
      print("登录成功");
      //跳转到homepage
      NavigatorUtil.gotoHomePage(context);
    }
  }

  void _registeredtUser() async {
    //注册用户的逻辑
    //跳转到登录页面
    var uri = Uri.parse(
      "https://api.devio.org/uapi/swagger-ui.html#/Account/registrationusingpost",
    );

    if (!await launchUrl(uri, mode: LaunchMode.externalApplication)) {
      throw '无法启动$uri';
    }
  }
}
